package com.android.fragmentdemo.defineviews
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView

class OverScrollRecyclerView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : RecyclerView(context, attributeSet) {


    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d(TAG, "$TAG---onInterceptTouchEvent---MotionEvent.ACTION_DOWN---parent---${parent::class.java.simpleName}")
                //通知父层ViewGroup不要拦截点击事件
                //必须调用parent.requestDisallowInterceptTouchEvent(true)而不是requestDisallowInterceptTouchEvent(true)
                //在此处当前View的parent是FrameLayout,这样才能保持事件链式传递.
                parent.requestDisallowInterceptTouchEvent(true)
            }
        }
        Log.d(TAG, "$TAG---super.onInterceptTouchEvent(ev)---${super.onInterceptTouchEvent(ev)}")
        return super.onInterceptTouchEvent(ev)
    }

    override fun offsetChildrenVertical(dy: Int) {
        super.offsetChildrenVertical(dy)
        val condition = dy == 0
        if (condition) {
            parent.requestDisallowInterceptTouchEvent(false)
        }
        Log.d(TAG, "$TAG ---dy:${dy} ---condition:${condition}")
    }

    companion object {
        const val TAG = "OverScrollRecyclerView"
    }
}